package com.a.lib_ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.a.lib_ui.R;
import com.a.lib_ui.bean.GridItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridItemAdapter extends ArrayAdapter<GridItem> {

    private static final String TAG = GridItemAdapter.class.getSimpleName();

    private Context context;

    private List<GridItem> gridItemList;

    private int layoutResourceID;

    public GridItemAdapter(Context context, int resource, List<GridItem> gridItemList) {
        super(context, resource, gridItemList);

        this.context = context;
        this.layoutResourceID = resource;
        this.gridItemList = gridItemList;
    }

    private Map<Integer, Boolean> map = new HashMap<>();

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceID, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.tv_tab);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        GridItem item = gridItemList.get(position);
        holder.textView.setText(item.getTitle());

        if (gridItemList.get(0).isChecked()) {  //默认选中"全部"
            map.put(0, true);
        }

        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "position为" + position);

                item.setChecked(!item.isChecked());

                if (item.isChecked()) {
                    map.put(position, true);

                } else {
                    map.remove(position);

                }

                if (position == 0) {
                    map.clear();
                    map.put(position, true);
                    item.setChecked(true);
                    Log.d(TAG, "");
                    for (int i = 1; i < gridItemList.size(); i++) {
                        GridItem tItem = gridItemList.get(i);
                        tItem.setChecked(false);
                    }
                } else {
                    map.remove(0);
                    gridItemList.get(0).setChecked(false);
                }


                notifyDataSetChanged();
            }
        });
        if (map != null && map.containsKey(position)) {
            holder.textView.setBackgroundResource(R.drawable.grid_item_change);
            holder.textView.setTextColor(context.getResources().getColor(R.color.white));


        } else {
            holder.textView.setBackgroundResource(R.drawable.grid_item);
            holder.textView.setTextColor(context.getResources().getColor(R.color.darkgrey));
        }
        return convertView;
    }

    private class ViewHolder {
        TextView textView;

    }

    public Map<Integer, Boolean> getMap() {
        return this.map;
    }

}
